<?php
namespace Destroyer\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Rule;
use Destroyer\Models\Menu;

class MenuCreateRequest extends FormRequest
{

    protected $stopOnFirstFailure = false;

    public function rules(): array
    {
        // $unique = Menu::unique(Menu::class);

        if( $this->isType('B') ){
            return [
                'parent_id' => ['required', 'integer'],
                'type'      => ['required', 'in:M,B'],
                'title'     => ['required', 'string', 'max:50'],
                'code'      => ['required', 'string', 'max:50'],
                'sort'      => ['required', 'integer', 'between:0,65535'],
                'is_hidden' => ['required', 'boolean'],
            ];
        }

        // 兜底 默认类型菜单M
        return [
            'parent_id' => ['required', 'integer'],
            'type'      => ['required', 'in:M,B'],
            'title'     => ['required', 'string', 'max:50'],
            'code'      => ['required', 'string', 'max:50'],
            'sort'      => ['required', 'integer', 'between:0,65535'],
            'is_hidden' => ['required', 'boolean'],
        ];
    }

    public function attributes()
    {
        return [
            'parent_id' => '上级菜单',
            'type'      => '菜单类型',
            'title'     => '菜单名称',
            'code'      => '权限标识',
        ];
    }

    public function messages()
    {
        return [];
    }

    public function isType($type)
    {
        return $this->input('type') == $type;
    }
}
